package com.dfdchem.imd.kaiwu.util;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;

import java.lang.reflect.Type;
import java.util.List;

public class SPUtil {
    private final SharedPreferences mSp;
    private final SharedPreferences.Editor mEditor;
    @SuppressLint("CommitPrefEdits")
    public SPUtil (Context context) {
        mSp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
        mEditor = mSp.edit();
    }

    /*--------------- 读 ---------------*/
    /**
     * 读取字符信息
     */
    public String getString(String key, String defValue) {
        return mSp.getString(key, defValue);
    }

    /**
     * 读取整型
     */
    public int getInt(String key, int defValue) {
        return mSp.getInt(key, defValue);
    }

    /**
     * 读取布尔型
     */
    public boolean getBoolean(String key, boolean defValue) {
        return mSp.getBoolean(key, defValue);
    }

    /**
     * 读取浮点型
     */
    public float getFloat(String key, float defValue) {
        return mSp.getFloat(key, defValue);
    }

    /*--------------- 写 ---------------*/
    /**
     * 写入string
     * @param key
     * @param value
     */
    public boolean putString(String key, String value) {
        mEditor.remove(key);
        mEditor.putString(key, value);
        return mEditor.commit();
    }
    /**
     * 写入int
     * @param key
     * @param value
     */
    public boolean putInt(String key, int value) {
        mEditor.remove(key);
        mEditor.putInt(key, value);
        return mEditor.commit();
    }

    /**
     * 写入boolean
     * @param key
     * @param value
     */
    public boolean putBoolean(String key, boolean value) {
        mEditor.remove(key);
        mEditor.putBoolean(key, value);
        return mEditor.commit();
    }

    /**
     * 写入float
     * @param key
     * @param value
     */
    public boolean putFloat(String key, float value) {
        mEditor.remove(key);
        mEditor.putFloat(key, value);
        return mEditor.commit();
    }

    /**
     * 通用写入Object
     * @param key
     * @param value
     */
    public <T> boolean putObject(String key, T t) {
        if (StringUtil.isEmpty(key)) {
            return false;
        }
        String data = JSON.toJSONString(t);
        mEditor.remove(key);
        mEditor.putString(key, data);
        return mEditor.commit();
    }

    /**
     * 通用读取Object
     * @param key
     * @param value
     */
    public <T> T getObject(String key, Class<T> clazz) {
        if (StringUtil.isEmpty(key)) {
            return null;
        }
        String data = mSp.getString(key, null);
        if (!JSON.isJsonCorrect(data)) {
            return null;
        }
        T t = JSON.parseObject(data, clazz);
        return t;
    }

    /**
     * 通用写入List<Object>
     * @param key
     * @param value
     */
    public <T> boolean putList(String key, T t) {
        if (StringUtil.isEmpty(key)) {
            return false;
        }
        String data = JSON.toJSONString(t);
        mEditor.remove(key);
        mEditor.putString(key, data);
        return mEditor.commit();
    }

    /**
     * 通用读取Object
     * @param key
     * @param value
     */
    public <T> List<T> getList(String key, Class<T> clazz) {
        if (StringUtil.isEmpty(key)) {
            return null;
        }
        String data = mSp.getString(key, null);
        if (!JSON.isJsonCorrect(data)) {
            return null;
        }
        Type type = new TypeReference<List<T>>(clazz){}.getType();
        List<T> t = JSONObject.parseObject(data, type);
        return t;
    }
}